<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠
// +----------------------------------------------------------------------
namespace app\xqhz\controller;

use app\xqhz\model\Project as ProjectModel;
use app\admin\controller\Admin;
/**
 * 项目控制器
 * 
 * @author 李忠
 * @createAt 2018-11-29 09:30:34

 *        
 */
class Project extends Admin {
	
	/**
	 * 列表页面
	 */
	public function index($title = null, $status = null) {
	 	cookie('__forward__', $this->request->url());

		// 数据列表
		$data_list = ProjectModel::lists($title,$status);
		$this->assign('data_list', $data_list);
		$this->assign('pager', $data_list->render());

		$this->assign('data_status', config('data_status'));
		$this->assign('status', $status);
		$this->assign('title', $title);
		$this->setMeta ('项目管理');
		return $this->fetch ();
	}
	// 新增项目
	public function add() {
		if ($this->request->isPost()) {
			// 表单数据
			$data = $this->request->post();
			// 验证
			$result = $this->validate($data, 'Project');
			if (true !== $result) {
				$this->error($result);
			}
			if ($data = ProjectModel::create($data)) {
				$this->log($data['id'], '新增项目成功');
				$this->success('新增项目成功', cookie('__forward__'));
			} else {
				$this->error('新增项目失败');
			}
		} else {
			$this->assign('info', null);
			$this->setMeta('新增项目');
			return $this->fetch();
		}
	}

	// 更新项目
	public function edit($id = null) {
		if ($this->request->isPost()) {
			// 表单数据
			$data = $this->request->post();
			// 验证
			$result = $this->validate($data, 'Project');
			if (true !== $result) {
				$this->error($result);
			}
			if ($data = ProjectModel::update($data)) {

				$this->log($data['id'], '更新项目成功');
				$this->success('更新项目成功', cookie('__forward__'));
			} else {
				$this->error('更新项目失败');
			}

		} else {
			$info = ProjectModel::get($id);

			$this->assign('info', $info);
			$this->setMeta('更新项目');
			return $this->fetch('add');
		}
	}

    /**
    * 禁用
    * @param int $id
    */
	public function forbid($id){
		if (ProjectModel::state($id, -1)){
			$this->log($id, '禁用项目成功');
			$this->success('禁用项目成功');
		}else{
			$this->error('禁用项目失败');
		}
	}

    /**
    * 恢复
    * @param int $id
    */
	public function resume($id){
		if (ProjectModel::state($id, -1)){
			$this->log($id, '启用项目成功');
		$this->success('启用项目成功');
		}else{
			$this->error('启用项目失败');
		}
	}

    /**
    * 删除
    * @param int $id
    */
	public function delete($id){
		if (ProjectModel::state($id, -1)){
			$this->log($id, '删除项目成功');
		$this->success('删除项目成功');
		}else{
			$this->error('删除项目失败');
		}
	}

}